package com.mew.lobby.handler.user;

import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.StringMessage;
import com.mew.common.util.StringUtils;
import com.mew.common.rest.bo.MewSession;
import com.mew.lobby.annotation.LobbyHandler;
import com.mew.lobby.net.LobbyHandlerMapCmdType;
import com.mew.lobby.net.MewHandler;
import com.mew.lobby.provide.UserFeignService;
import com.vdurmont.emoji.EmojiParser;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**修改名称
 * @author dylin
 * @create 2021/1/26 0026
 */
@Service
@Slf4j
@LobbyHandler(LobbyHandlerMapCmdType.NickName)
public class NickNameHandler implements MewHandler<StringMessage> {
    @Autowired
    private UserFeignService userFeignService;
    
    @Override
    public void handle(ZebraCommand<StringMessage> zebraCommand, MewSession session) {
        long userId = zebraCommand.getUserId();
       
        String nickName = EmojiParser.removeAllEmojis(zebraCommand.getRequest().getS());
        if (StringUtils.isEmpty(nickName)){
            zebraCommand.sendFailureCode(ResponseCode.ParamInvalid.code());
            return;
        }
        int code = userFeignService.updateNickname(userId,nickName);
        if(code== ResponseCode.Success.code()){
            zebraCommand.sendMessage(StringMessage.builder().s(nickName).build());
            return;
        }
        zebraCommand.sendFailureCode(code);
    }

}
